package com.yanlight.util;

import com.alibaba.fastjson.JSONObject;
import com.yanlight.app.dal.entity.AwardItem;
import com.yanlight.app.dal.entity.AwardSimpleItem;
import com.yanlight.support.AppConstants;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 抽奖工具类<br/>
 * 整体思想：
 * 奖品集合 + 概率比例集合
 * 将奖品按集合中顺序概率计算成所占比例区间，放入比例集合。并产生一个随机数加入其中，排序。</br>
 * 排序后，随机数落在哪个区间，就表示那个区间的奖品被抽中。</br>
 * 返回的随机数在集合中的索引，该索引就是奖品集合中的索引。</br>
 * 比例区间的计算通过概率相加获得。
 *
 * @author irving
 * @version MARK 0.0.1
 * @since 2017年7月23日 下午9:48:23
 */
public class DrawLotteryUtil {

    public static int drawAwardItem(List<AwardItem> AwardItemList) {

        if (null != AwardItemList && AwardItemList.size() > 0) {
            List<Double> orgProbList = new ArrayList<Double>(AwardItemList.size());
            for (AwardItem AwardItem : AwardItemList) {
                //按顺序将概率添加到集合中
                orgProbList.add(AwardItem.getProb());
            }

            return draw(orgProbList);

        }
        return -1;
    }

    public static int drawAwardItem() {
        List<AwardItem> awardList = getAwardList();
        return drawAwardItem(awardList);
    }

    public static AwardItem getAwardItem(int index) {
        return getAwardList().get(index);
    }

    //返回不中奖的索引
    public static int getNoPrizeIndex() {
        return 0;
    }

    private static int draw(List<Double> AwardItemProbList) {

        List<Double> sortRateList = new ArrayList<Double>();

        // 计算概率总和
        Double sumRate = 0D;
        for (Double prob : AwardItemProbList) {
            sumRate += prob;
        }

        if (sumRate != 0) {
            double rate = 0D;   //概率所占比例
            for (Double prob : AwardItemProbList) {
                rate += prob;
                // 构建一个比例区段组成的集合(避免概率和不为1)
                sortRateList.add(rate / sumRate);
            }

            // 随机生成一个随机数，并排序
            double random = Math.random();
            sortRateList.add(random);
            Collections.sort(sortRateList);

            // 返回该随机数在比例集合中的索引
            return sortRateList.indexOf(random);
        }

        return -1;
    }

    public static List<AwardSimpleItem> getSimpleAwardList() {
        List<AwardSimpleItem> simpleArrayList = null;

        String simpleAwardList = JedisUtil.get(AppConstants.SIMPLE_AWARD_LIST);
        if (simpleAwardList != null) {
            simpleArrayList = JSONObject.parseArray(simpleAwardList, AwardSimpleItem.class);
        }

        if (simpleArrayList == null || simpleArrayList.isEmpty()) {
            setupAwardList();
            return getSimpleAwardList();
        } else {
            return simpleArrayList;
        }
    }

    public static List<AwardItem> getAwardList() {
        List<AwardItem> itemArrayList = null;
        String awardList = JedisUtil.get(AppConstants.AWARD_LIST);
        if (awardList != null) {
            itemArrayList = JSONObject.parseArray(awardList, AwardItem.class);
        }

        if (itemArrayList == null || itemArrayList.isEmpty()) {
            setupAwardList();
            return getAwardList();
        } else {
            return itemArrayList;
        }
    }

    public static void setupAwardList() {

        List<AwardItem> itemArrayList = new ArrayList<>();

        itemArrayList.add(new AwardItem("谢谢参与", "#FFF4D6", 20.0, 0, 0.0));
        itemArrayList.add(new AwardItem("0.3元现金", "#FFFFFF", 60.0, 1, 0.3));

        itemArrayList.add(new AwardItem("谢谢参与", "#FFF4D6", 20.0, 0, 0.0));
        itemArrayList.add(new AwardItem("1元红包", "#FFFFFF", 30.0, 2, 1.0));

        itemArrayList.add(new AwardItem("谢谢参与", "#FFF4D6", 20.0, 0, 0.0));
        itemArrayList.add(new AwardItem("5元红包", "#FFFFFF", 1.0, 2, 5.0));

        itemArrayList.add(new AwardItem("谢谢参与", "#FFF4D6", 20.0, 0, 0.0));
        itemArrayList.add(new AwardItem("10元红包", "#FFFFFF", 0.1, 2, 10.0));

        itemArrayList.add(new AwardItem("谢谢参与", "#FFF4D6", 20.0, 0, 0.0));
        itemArrayList.add(new AwardItem("30元红包", "#FFFFFF", 0.01, 2, 20.0));

        List<AwardSimpleItem> simpleArrayList = new ArrayList<>();
        for (AwardItem item : itemArrayList) {
            simpleArrayList.add(new AwardSimpleItem(item.getName(), item.getColor()));
        }

        JedisUtil.set(AppConstants.AWARD_LIST, JSONObject.toJSONString(itemArrayList));
        JedisUtil.set(AppConstants.SIMPLE_AWARD_LIST, JSONObject.toJSONString(simpleArrayList));
    }

//    public static void main(String[] args) {
//
//        for (int i = 0; i < 100; i++) {
//            int index = drawAwardItem();
//            System.out.println(getAwardList().get(index).getName() + "    :   " + (index + 1));
//        }
//    }

}

